package cn.com.coding.app;

import cn.com.coding.common.lang.ResT;
import cn.com.coding.common.utils.PageUtil;
import cn.com.coding.system.domin.SysRoleEntity;
import cn.com.coding.system.domin.SysRoleMenuEntity;
import cn.hutool.core.map.MapUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * @author mxz
 */
@RestController
@RequestMapping("cat/sysRole")
public class SysRoleController extends BaseController {

    /**
     * 列表
     *
     * @param params
     * @return
     */
    @RequestMapping("/list")
    public ResT list(@RequestParam Map<String, Object> params) {
        PageUtil page = sysRoleService.queryPage(params);

        return ResT.ok().put("page", page);
    }

    /**
     * 信息
     *
     * @param pid
     * @return
     */
    @RequestMapping("/info/{pid}")
    public ResT info(@PathVariable("pid") String pid) {
        SysRoleEntity sysRole = sysRoleService.getById(pid);

        List<SysRoleMenuEntity> sysRoleMenuEntityList = sysRoleMenuService.list(new QueryWrapper<SysRoleMenuEntity>().eq("role_pid", pid));

        List<String> list = sysRoleMenuEntityList.stream().map(e -> e.getMenuPid()).collect(Collectors.toList());

        sysRole.setMenuPids(list);

        return ResT.ok().setData(MapUtil.builder()
                .put("sysRole", sysRole)
                .build());
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public ResT save(@RequestBody SysRoleEntity sysRole) {
        sysRoleService.save(sysRole);

        return ResT.ok();
    }

    /**
     * 修改
     *
     * @param sysRole
     * @return
     */
    @RequestMapping("/update")
    public ResT update(@RequestBody SysRoleEntity sysRole) {
        sysRoleService.updateById(sysRole);

        return ResT.ok();
    }

    /**
     * 分配权限
     *
     * @param rolePid
     * @param menuPids
     * @return
     */
    @RequestMapping("/perm/{rolePid}")
    public ResT perm(@PathVariable("rolePid") String rolePid, @RequestParam("menuPids") List<String> menuPids) {
        List<SysRoleMenuEntity> sysRoleMenus = new ArrayList<>();

        menuPids.forEach(menuId -> {
            SysRoleMenuEntity roleMenu = new SysRoleMenuEntity();
            roleMenu.setMenuPid(menuId);
            roleMenu.setRolePid(rolePid);

            sysRoleMenus.add(roleMenu);
        });

        // 先删除原来的记录，再保存新的
        sysRoleMenuService.remove(new QueryWrapper<SysRoleMenuEntity>().eq("role_pid", rolePid));
        sysRoleMenuService.saveBatch(sysRoleMenus);

        // 删除缓存
        sysUserService.clearUserAuthorityInfoByRolePid(rolePid);
        return ResT.ok();
    }

    /**
     * 删除
     *
     * @param pids
     * @return
     */
    @RequestMapping("/delete")
    public ResT delete(@RequestBody String[] pids) {
        sysRoleService.removeByIds(Arrays.asList(pids));

        return ResT.ok();
    }

}
